﻿using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class ReportService
    {
        /// <summary>
        /// 报表数据持久层
        /// </summary>
        private readonly ReportDAO dao = new ReportDAO();
         /// <summary>
        /// 根据机构获取车辆使用次数
        /// </summary>
        /// <param name="orgz">机构</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="tag">报表类型</param>
        /// <returns></returns>
        public IList<ReportModel> GetUseCountData(int orgz, DateTime start, DateTime end, int tag)
        {
            return this.dao.GetUseCountData(orgz, start, end, tag);
        }
         /// <summary>
        /// 按照驾驶员统计
        /// </summary>
        /// <param name="orgz">机构</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <returns></returns>
        public IList<DriverModel> GetDriverData(int orgz, DateTime start, DateTime end)
        {
            var list = dao.GetDriverData(orgz, start, end).GroupBy(it => it.CarNo);
            IList<DriverModel> data = new List<DriverModel>();
            foreach (var item in list)
            {
                DriverModel ent = new DriverModel();
                ent.Name = item.Key;
                var uc = item.Where(it => it.CarType == 0).FirstOrDefault();
                if (uc != null)
                    ent.CarUseCount = uc.UseCount;
                var ac = item.Where(it => it.CarType == 1).FirstOrDefault();
                if (ac != null)
                {
                    ent.AccidentCount = ac.UseCount;
                    ent.AccidentMoney = ac.Money;
                }
                var fill = item.Where(it => it.CarType == 2).FirstOrDefault();
                if (fill != null)
                {
                    ent.FillGasolineCount = fill.UseCount;
                    ent.FillGasolineMoney = fill.Money;
                }
                var ill = item.Where(it => it.CarType == 3).FirstOrDefault();
                if (ill != null)
                {
                    ent.IllegalCount = ill.UseCount;
                    ent.IllegalMoney = ill.Money;
                }
                var mn = item.Where(it => it.CarType == 4).FirstOrDefault();
                if (mn != null)
                {
                    ent.MaintainCount = mn.UseCount;
                    ent.MaintainMoney = mn.Money;
                }
                var mc = item.Where(it => it.CarType == 5).FirstOrDefault();
                if (mc != null)
                {
                    ent.MaintenanceCount = mc.UseCount;
                    ent.MaintenanceMoney = mc.Money;
                }
                data.Add(ent);
            }
            return data;
        }
    }
}
